home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / dblib.h < prev    next >
C/C++ Source or Header  |  1991-05-04  |  27KB  |  820 lines

  1. #ifndef DBLIB_HEADER
  2.     #define DBLIB_HEADER
  3. #include "wtwg.h"
  4. #include "string.h"
  5. #include <iostream.h>
  6. #include <alloc.h>
  7. #include <dos.h>
  8.  
  9. /* farmemcpy()
  10.  * in large models this is just memcpy
  11.  * but in small models it's movedata()
  12.  */
  13. #undef LARGE_DATA_MODEL
  14. #ifdef __LARGE__
  15.     #define LARGE_DATA_MODEL
  16. #endif
  17. #ifdef __COMPACT__
  18.     #define LARGE_DATA_MODEL
  19. #endif
  20. #ifdef __HUGE__
  21.     #define LARGE_DATA_MODEL
  22. #endif 
  23.  
  24.  
  25.  
  26.  
  27. #ifdef LARGE_DATA_MODEL
  28.     #define farmemcpy(aa,bb,cc)  memcpy (aa,bb,cc)
  29. #else
  30.  
  31.     /* model is small or medium - need a far * equivalent of
  32.      * memcpy, write, read, memcmp
  33.      */
  34.     #define farmemcpy(dest,src,num)  \
  35.     movedata (FP_SEG(src), FP_OFF(src), FP_SEG(dest), FP_OFF(dest), num )
  36.  
  37. #endif    /* memcpy */
  38.  
  39.  
  40.  
  41. // min & max. - these are undefined in TurboC++
  42. #ifndef min
  43.     #define min(x,y)   (( (x) < (y) ) ? (x) : (y))
  44. #endif
  45. #ifndef max
  46.     #define max(x,y)   (( (x) > (y) ) ? (x) : (y))
  47. #endif
  48.  
  49.  
  50.  
  51.  
  52.  
  53. /* array index routines.
  54.  *
  55.  * roll_up, roll_down
  56.  *    returns the next/prev index to an array of max items
  57.  *     arranged in a circle  0 -> 1 -> 2 -> ... -> max -> 0 etc...
  58.  * inch_up, inch_down
  59.  *    increases or decreases the index but stays within array bounds
  60.  */
  61. #define roll_up(n, max)      ( (n) == (max)-1 ? 0       : (n)+1 )
  62. #define roll_down(n, max)    ( (n) ==  0      ? (max)-1 : (n)-1 )
  63. #define inch_up(n, max)        ( (n) == (max)-1 ? (n)     : (n)+1 )
  64. #define inch_down(n, max)    ( (n) ==  0      ? 0       : (n)-1 )
  65.  
  66.  
  67.  
  68.  
  69.  
  70. // NORMALIZE macros.
  71. //    Far pointer normalization routine
  72. //        Arithmetic on far pointers only affects the offset,
  73. //        does not carry into the segment portion.
  74. //     to avoid overflow errors,
  75. //        must convert seg:off pair to place as much
  76. //         of the address as possible into the segment part.
  77. //
  78. //     NORMALIZE () - unconditionally normalize a far pointer.
  79. //      _NORMALIZE () - normalize a data pointer IF memory model requires it
  80. //
  81. //  use _NORMALIZE for pointers not explicitly declared far.
  82. //
  83. //    NOTE:  MODIFIED FOR C++  which must cast the result to the appropriate type
  84. // 
  85. #define NORMALIZE(fp, type)        \
  86.    fp = (type) MK_FP ( ( FP_SEG(fp) + (FP_OFF(fp)>>4) ),  \
  87.         ((unsigned)(FP_OFF(fp))& 0x000f) )
  88.  
  89.  
  90.  
  91.  
  92.  
  93. #ifdef  __COMPACT__
  94.     #define _NORMALIZE(fp,type)  NORMALIZE(fp,type)
  95. #endif
  96. #ifdef  __LARGE__
  97.     #define _NORMALIZE(fp,type)  NORMALIZE(fp,type)
  98. #endif
  99. #ifdef __SMALL__
  100.     #define _NORMALIZE(fp,type)
  101. #endif
  102. #ifdef __MEDIUM__
  103.     #define _NORMALIZE(fp,type)
  104. #endif
  105. #ifdef __HUGE__
  106.     #define _NORMALIZE(fp,type)
  107. #endif
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /* the next set of definitions allow use of Turbo C's ability to
  122.  *  generate in-line interrupts, while maintaining compatibility
  123.  *  with compilers that have an int86() function and a union REGS.
  124.  *
  125.  *  To use this technique, ALWAYS load the registers in inverse
  126.  *  alphabetical order (DX first, then CX, BX and AX last)
  127.  *  and don't try to do any complex computations in the lines
  128.  *  that access the registers.
  129.  *     WRONG, for 3 reasons:
  130.  *        _AH = 1;
  131.  *        _DX = value1 + 3*value2;    ===> TurboC uses AX
  132.  *        geninterrupt ( variable_int_number );  ditto
  133.  *    RIGHT, but useless:
  134.  *        value3 = value1 + 3*value2;
  135.  *        if ( int_number = 0x01 )
  136.  *            {
  137.  *            _DX = value3;
  138.  *            _AX = 1;
  139.  *            geninterrupt ( 0x01 );
  140.  *            }
  141.  *        else    {
  142.  *            _DX = value3;
  143.  *            _AX = 1;
  144.  *            geninterrupt ( 0x02 );
  145.  *            }
  146.  *
  147.  */
  148.     #ifndef  __TURBOC__
  149.         #define PSEUDOREGS union REGS regs;
  150.         #define _AX    regs.x.ax
  151.         #define _AL    regs.h.al
  152.         #define _AH    regs.h.ah
  153.         #define _BX    regs.x.bx
  154.         #define _BL    regs.h.bl
  155.         #define _BH    regs.h.bh
  156.         #define _CX    regs.x.cx
  157.         #define _CL    regs.h.cl
  158.         #define _CH    regs.h.ch
  159.         #define _DX    regs.x.dx
  160.         #define _DL    regs.h.dl
  161.         #define _DH    regs.h.dh
  162.         #define _FLAGS    regs.x.flags
  163.  
  164.         #define geninterrupt(intno)    int86((intno), ®s, ®s)
  165.  
  166.     #else
  167.         #define    PSEUDOREGS
  168.     #endif    /* end of redifinning pseudoregs and interrupts */
  169.  
  170.     //    generate inport and outport instructions inline
  171.     //  temp. fix pending TURBOC++ upgrade.
  172.     #ifndef outportb
  173.         #define outportb(portid, value)  __outportb__( (portid), (value) )
  174.         void     _Cdecl __outportb__(int __portid, unsigned char __value);
  175.  
  176.     #endif
  177.     #ifndef inportb
  178.         #define inportb(portid)  __inportb__( (portid) )
  179.         unsigned char    _Cdecl __inportb__(int __portid);
  180.     #endif
  181.  
  182. // wmatherr() -
  183. //        a routine to trap integer and floating point math errors.
  184. //        prevents program from crashing, which would leave system...
  185. //             in bizarre video modes, with expanded memory allocated, etc...
  186. //        SOURCE: wmatherr.cpp
  187. void wmatherr (void);
  188.  
  189.  
  190.  
  191. // STRINGPP.H - a string class for TurboC++
  192. //
  193. // D Blum 8/90 
  194. //
  195.  
  196.  
  197. class String {
  198.     public:
  199.             static     char caseSens;        // default = OFF  any NONZERO is ON
  200.     private:
  201.             int    n;            // strlen = numbytes not counting \0
  202.             char *s;        // the string.
  203.             void construct ( char *p );    // local utility for construction
  204.             void destruct ( );            // local utility for destruction
  205.                                         // source for both: STRPP.CPP
  206.             // Utility functions called by functions in String::
  207.             //    the utilities include copy, comparison, scanning, allocation
  208.             //                                                // source files:
  209.             static int cmp (char *a, char *b);                // STRPPCMP.CPP
  210.             static int memcmp (char *a, char *b, int nb);    // STRPPMCP.CPP
  211.             static int chrcmp ( char a, char b );            // STRPPCHC.CPP
  212.             static int findstr ( char *a, int na, char *b );// STRPPFNS.CPP    
  213.             static int findchr ( char *a, int na, char c );    // STRPPFNC.CPP
  214.             void       strpad ( );                            // STRPPPAD.CPP
  215.             void          slide ( int start, int stop );        // STRPPSLD.CPP 
  216.     public: 
  217.             // Constructors: make String from a String, a char *ptr, or a char
  218.             //                also can make fixed length strings.
  219.                 String()    { n=0; s=NULL;};
  220.                 String(int);                    // make a fixed len String 
  221.                                                 // init to all spaces.
  222.                                                 // source: STRPPFIX.CPP
  223.                 String ( char *ptr ){n = strlen (ptr); construct (ptr); };        
  224.                 String ( String& str ){ n= str.n; construct ( str.s ); };
  225.             // Copy constructor utility.
  226.                 private: void copy( int l, char *p );    // source: STRPP.CPP
  227.             public:
  228.                 void    assign ( char *data );    // assign ownership of data
  229.                                                 // data MUST have been malloc'd
  230.                                                 // and will be free'd by String
  231.                                                 //        BE CAREFULL !!!!!
  232.                                                 // STRPPASN.CPP
  233.                 String& operator=(String& source)        
  234.                     {
  235.                     copy ( source.n, source.s );
  236.                     return *this;
  237.                     };            // end assignemt operator
  238.                 String& operator=(char *ptr)
  239.                     {
  240.                     copy ( strlen(ptr), ptr );
  241.                     return *this;
  242.                     };
  243.                 String& operator= (char c)
  244.                     {
  245.                     copy ( 1, &c );
  246.                     return *this;
  247.                     };
  248.             // Destructor
  249.                 ~String ( ) {destruct();};        // source: STRPP.CPP
  250.                 
  251.             // comparison operators.
  252.             
  253.             friend int operator==(String& a, String &b)
  254.                 {return (String::cmp (a.s, b.s)==0);};
  255.             friend int operator>=(String& a, String &b)
  256.                 {return (String::cmp (a.s, b.s)>=0);};    
  257.             friend int operator<=(String& a, String &b)
  258.                 {return (String::cmp (a.s, b.s)<=0);};    
  259.             friend int operator>(String& a, String &b)
  260.                 {return (String::cmp (a.s, b.s)>0);};    
  261.             friend int operator<(String& a, String &b)
  262.                 {return (String::cmp (a.s, b.s)<0);};    
  263.             friend int operator==(char *a, String &b)
  264.                 {return (String::cmp (a, b.s)==0);};
  265.             friend int operator>=(char *a, String &b)
  266.                 {return (String::cmp (a, b.s)>=0);};    
  267.             friend int operator<=(char *a, String &b)
  268.                 {return (String::cmp (a, b.s)<=0);};    
  269.             friend int operator>(char *a, String &b)
  270.                 {return (String::cmp (a, b.s)>0);};    
  271.             friend int operator<(char *a, String &b)
  272.                 {return (String::cmp (a, b.s)<0);};    
  273.             friend int operator==(String& a, char *b)
  274.                 {return (String::cmp (a.s, b)==0);};
  275.             friend int operator>=(String& a, char *b)
  276.                 {return (String::cmp (a.s, b)>=0);};    
  277.             friend int operator<=(String& a, char *b)
  278.                 {return (String::cmp (a.s, b)<=0);};    
  279.             friend int operator>(String& a, char *b)
  280.                 {return (String::cmp (a.s, b)>0);};    
  281.             friend int operator<(String& a, char *b)
  282.                 {return (String::cmp (a.s, b)<0);};    
  283.                 
  284.                 
  285.         // CONCATENAT